//公共方法

//弹窗
$(document).ready(function() {
    $(".container").on("click", function(event) {
        event.preventDefault();
        $(".fulled").fadeIn();
    });
    $(".close").on("click", function(event) {
        if ($(event.target).is(".close") || $(event.target).is(".fix-box")) {
            //alert(123)
            event.preventDefault();
            $(".fulled").hide();
        }
    });
    $(document).keyup(function(event) {
        if (event.which == "27") {
            $(".fulled").fadeOut();
        }

    })
});
//注册
function reg() {
    var username = $("#username2").val(),
        password = $("#pwd2").val(),
        repwd = $("#repwd").val(),
        phone = $("#phone").val(),
        email = $("#eml").val();
    if(username=="" || username==null){
        $("#names").empty();
        $("#username2").after("<span id='names' style='color: red'>用户名不能为空</span>");
        return false;
    }if(password=="" || password==null){
        $("#pwd3").empty();
        $("#pwd2").after("<span id='pwd3' style='color: red'>密码不能为空</span>");return false;
    }if(repwd=="" || repwd==null){
        $("#repwd2").empty();
        $("#repwd").after("<span id='repwd2' style='color: red'>确认密码不能为空</span>");return false;
    }
    else if(repwd !=password){
        $("#repwd2").empty();
        $("#repwd").after("<span id='repwd2' style='color: red'>两次密码不一致</span>");return false;
    }
    if(phone=="" || phone==null || phone.length!=11){
        $("#phone2").empty();
        $("#phone").after("<span id='phone2' style='color: red'>请正确填写手机号</span>");return false;
    }
    if(email=="" || email==null){
        $("#eml2").empty();
        $("#eml").after("<span id='eml2' style='color: red'>邮箱不能为空</span>");return false;
    }else {
        post_data ={
            username:username,
            password:password,
            phone:phone,
            email:email
        };
        $.post('doAction.php?act=register',post_data,function (data) {
            console.log(data);
            if (data==200){
                alert("注册成功，请在左侧登录界面登录");
                window.location = 'main.php';
            }else {
                alert("注册失败，用户名已被注册");
            }
        })
    }
}
//登录
function login(type) {
    var username = $("#username").val(),
        password = $("#pwd").val();
    if(username=="" || username==null){
        $("#names").empty();
        $("#username").after("<span id='names' style='color: red'>用户名不能为空</span>");return false;
    }if(password=="" || password==null){
        $("#password2").empty();
        $("#pwd").after("<span id='password2' style='color: red'>密码不能为空</span>");return false;
    }else {
        post_data ={
            username:username,
            password:password
        };
        $.post('doAction.php?act=dologin',post_data,function (data) {
            console.log(data);
            if (data==400){
                alert("账户或密码错误");
            }else {
                alert("登录成功");
                if (data==1 && type!=2){
                    window.location = 'admin.php';
                }else if (type==2){
                    window.location = 'aduser.php';
                }
                else {
                    window.location = 'index.php';
                }
            }
        })
    }
}
//跳转到注册页面
function zhuce() {
    window.location = 'regist.html';
}
//跳转到用户管理页面
function admin(id,identity) {
    if(id==''||id==null){
        alert('请先登录后操作');
    }else {
        if (identity==1){
            window.location = 'admin.php';
        }else {
            alert('您没有管理员权限');
        }
    }
}
//上传文件
function upload_file() {
    var fileM=document.querySelector("#file");
    var fileObj = fileM.files[0];
    var formData = new FormData();
    formData.append('file', fileObj);
    $.ajax({
        url:"doAction.php?act=up_file",
        type:"post",
        data:formData,
        processData:false,
        contentType:false,
        success:function(res){
            //转化json的值
            if(typeof res == 'string') {
                res = JSON.parse(res);
            }
            if(res.code==200){
                alert("上传成功！");
                console.log(res);
                $("#file_name").attr("value",res.name);
            }else {
                alert(res);
            }

        },
        error:function(err){
            alert("网络连接失败,稍后重试",err);
        }
    })
}
//添加实验、练习、资料
function add_kecheng(table) {
    var title = $("#title").val(),
        type = $("#type").val(),
        file = $("#file_name").val();
    if(title=="" || title==null){
        $("#titles").empty();
        $("#title").after("<span id='titles' style='color: red'>名称不能为空</span>");
        return false;
    }if(file=="" || file==null){
        $("#file_contents").empty();
        $("#file").after("<span id='file_contents' style='color: red'>请上传文件</span>");
        return false;
    }
    else {
        post_data={
            title:title,
            type:type,
            file_url:file,
            table:table
        };
        $.post('doAction.php?act=add_kecheng',post_data,function (data) {
            if (data==200){
                alert('添加成功');
            }else {
                alert(data);
            }
        })
    }
}
//删除实验、练习、资料、公告、留言、用户等
function del(table,id) {
    post_data={
        id:id,//对应数据id
        del:0,//删除
        table:table//数据表
    };
    $.post('doAction.php?act=del',post_data,function (data) {
        if (data==200){
            alert('删除成功');
        }else {
            alert(data);
        }
    })
}
//修改实验、联系、资料
function update_kecheng(table,url) {
    var title = $("#title").val(),
        type = $("#type").val(),
        id = $("#id").val(),
        file = $("#file_name").val();
    if(title=="" || title==null){
        $("#titles").empty();
        $("#title").after("<span id='titles' style='color: red'>名称不能为空</span>");
        return false;
    }
    else {
        post_data={
            title:title,
            type:type,
            file_url:file,
            table:table,
            id:id
        };
        $.post('doAction.php?act=update_kecheng',post_data,function (data) {
            if (data==200){
                alert('修改成功');
                window.location = url;
            }else {
                alert(data);
            }
        })
    }
}
//将要修改的参数传递到修改弹窗，实验、练习、资料
function xiugai(id,title,type,url) {
    $('#id').val(id);
    $('#title').val(title);
    $('#type').val(type);
    $("#file_name").attr("value",url);
}
//添加留言、回复
function add_reply(id) {
    var content = $("#content").val();
    if(content=="" || content==null){
        $("#titles").empty();
        $("#content").after("<span id='titles' style='color: red;margin-left: -420px'>内容不能为空</span>");
        return false;
    }
    else {
        post_data={
            content:content,
            pid:id
        };
        $.post('doAction.php?act=add_reply',post_data,function (data) {
            if (data==200){
                if(id!=0){
                    alert('回复成功');
                    window.location = 'reply.php?id='+id;
                }else {
                    alert('留言成功');
                    window.location = 'message.php';
                }

            }else {
                alert(data);
            }
        })
    }
}
//找回密码
function find_pwd() {
    var username = $("#username").val(),
        password = $("#pwd").val(),
        email = $("#email").val(),
        repwd = $("#repwd").val();
    if(username=="" || username==null){
        $("#names").empty();
        $("#username2").after("<span id='names' style='color: red'>用户名不能为空</span>");
        return false;
    }if(email=="" || email==null){
        $("#emails").empty();
        $("#email").after("<span id='emails' style='color: red'>用户名不能为空</span>");
        return false;
    }if(password=="" || password==null){
        $("#pwd3").empty();
        $("#pwd2").after("<span id='pwd3' style='color: red'>密码不能为空</span>");return false;
    }if(repwd=="" || repwd==null){
        $("#repwd2").empty();
        $("#repwd").after("<span id='repwd2' style='color: red'>确认密码不能为空</span>");return false;
    }
    else if(repwd !=password){
        $("#repwd2").empty();
        $("#repwd").after("<span id='repwd2' style='color: red'>两次密码不一致</span>");return false;
    }else {
        post_data ={
            username:username,
            password:password,
            email:email
        };
        $.post('doAction.php?act=find_pwd',post_data,function (data) {
            console.log(data);
            if (data==200){
                alert("修改密码成功");
                window.location = 'main.php';
            }else {
                alert("账户或邮箱错误");
            }
        })
    }
}